import os
from app.models.upload_model import Sample
from config import ALIYUN_ACCESS_KEY_ID, ALIYUN_ACCESS_KEY_SECRET, WORKSPACE_ID
from app.models.upload_model import AiUploadFile
from sqlalchemy.orm import Session
from sqlalchemy import SmallInteger  # 导入 SmallInteger 类型
from app.database.db_setup import get_db
from fastapi import Depends
def apply_file_upload_lease(
    category_id: str,
    file_name: str,
    size_in_bytes: str,
    md5: str,
):
    try:
        if not ALIYUN_ACCESS_KEY_ID or not ALIYUN_ACCESS_KEY_SECRET or not WORKSPACE_ID:
            return {"error": "请设置 ALIYUN_ACCESS_KEY_ID 和 ALIYUN_ACCESS_KEY_SECRET 和 WORKSPACE_ID 环境变量"}

        result = Sample.call_api(
            ALIYUN_ACCESS_KEY_ID,
            ALIYUN_ACCESS_KEY_SECRET,
            category_id,
            WORKSPACE_ID,
            file_name,
            md5,
            size_in_bytes
        )
        return result
    except FileNotFoundError:
        return {"error": f"文件 {file_name} 未找到"}
    except Exception as e:
        return {"error": str(e)}


def create_ai_upload_file_record(
    script_id:int,
    md5: str,
    cat_id: int,
    CategoryId: str,
    is_set: int,
    size_in_bytes:int,
    file_name: str,
    file_path: str,
    absolute_file_path: str,
    add_time: int,
    db: Session
):
    new_file_record = AiUploadFile(
        script_id=script_id,
        md5=md5,
        cat_id=cat_id,
        CategoryId=CategoryId,
        size_in_bytes=size_in_bytes,
        file_path=file_path,
        file_name=file_name,
        absolute_file_path=absolute_file_path,
        add_time=add_time,
    )
    try:
        db.add(new_file_record)
        db.commit()
        db.refresh(new_file_record)
        return new_file_record
    except Exception as e:
        db.rollback()
        raise e


async def get_ai_upload_file_list(
    db: Session,
):
    query = db.query(AiUploadFile)
    query = query.filter(AiUploadFile.is_set == 0)
    try:
        file_list = query.all()
        result = []
        for file in file_list:
            file_dict = {
                'id': file.id,
                'md5': file.md5,
                'cat_id': file.cat_id,
                'CategoryId': file.CategoryId,
                'file_path': file.file_path,
                'file_name': file.file_name,
                'size_in_bytes': file.size_in_bytes,
                'absolute_file_path': file.absolute_file_path,
                'add_time': file.add_time
            }
            result.append(file_dict)
        return result
    except Exception as e:
        return {"error": str(e)}
def update_ai_upload_file_record(
    db: Session,
    id: int,
    FileId: str = None,
    Parser: int = None,
    is_set: SmallInteger = None,
    set_time: int = None,
   
):
    file_record = db.query(AiUploadFile).filter(AiUploadFile.id == id).first()
    if not file_record:
        return {"error": f"未找到 ID 为 {id} 的记录"}
    if FileId is not None:
        file_record.FileId = FileId
    if Parser is not None:
        file_record.Parser = Parser
    if is_set is not None:
        file_record.is_set = is_set
    if set_time is not None:
        file_record.set_time = set_time
    try:
        db.commit()
        db.refresh(file_record)
        return file_record
    except Exception as e:
        db.rollback()
        return {"error": str(e)}